/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.celiosilva.simbanc.controller.util;

import javax.swing.JTextField;

/**
 *
 * @author celio@celiosilva.com
 */
public abstract class Conversor<D, P> {
    public static final Conversor<String,Integer> STRING_INTEGER = new Conversor<String, Integer>() {
        @Override
        public Integer converterPara(String d) {
            return d == null || d.length() == 0? null: Integer.valueOf(d);
        }
    };
    public static final Conversor<String, Enum> STRING_ENUM = new Conversor<String, Enum>() {
        @Override
        public Enum converterPara(String string) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    };

    public static final Conversor<JTextField,Integer> JTEXT_INTEGER = new Conversor<JTextField, Integer>() {
        @Override
        public Integer converterPara(JTextField d) {
            return Conversor.STRING_INTEGER.converterPara(d.getText());
        }
        @Override
        public JTextField converterDe(Integer p) {
            return new JTextField(p.toString());
        }
    };

    public abstract P converterPara(D d);
    public D converterDe(P p){
        return (D) (p == null ? "" : p.toString());
    }

    private Conversor(){}

}
